package com.ruoyi.fantasy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huanzhidadi.fantasy.bean.domain.FantasyNpc;
import com.huanzhidadi.fantasy.common.constant.RedisConstants;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.fantasy.domain.FantasyNpcPosition;
import com.ruoyi.fantasy.domain.FantasyNpcPositionBO;
import com.ruoyi.fantasy.mapper.FantasyNpcMapper;
import com.ruoyi.fantasy.utils.RedisUtil;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.SysUserRole;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fantasy.mapper.FantasyNpcPositionMapper;
import com.ruoyi.fantasy.service.IFantasyNpcPositionService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * npc位置 服务层实现
 * 
 * @author wanghui
 * @date 2019-08-02
 */
@Service
public class FantasyNpcPositionServiceImpl implements IFantasyNpcPositionService 
{
	@Resource
	private FantasyNpcPositionMapper fantasyNpcPositionMapper;

	@Resource
	private FantasyNpcMapper fantasyNpcMapper;

	@Autowired
	private RedisUtil redisUtil;

	/**
     * 查询npc位置信息
     * 
     * @param positionId npc位置ID
     * @return npc位置信息
     */
    @Override
	public FantasyNpcPosition selectFantasyNpcPositionById(Long positionId)
	{
	    return fantasyNpcPositionMapper.selectFantasyNpcPositionById(positionId);
	}
	
	/**
     * 查询npc位置列表
     * 
     * @param fantasyNpcPosition npc位置信息
     * @return npc位置集合
     */
	@Override
	public List<FantasyNpcPosition> selectFantasyNpcPositionList(FantasyNpcPosition fantasyNpcPosition)
	{
	    return fantasyNpcPositionMapper.selectFantasyNpcPositionList(fantasyNpcPosition);
	}
	
    /**
     * 新增npc位置
     * 
     * @param fantasyNpcPosition npc位置信息
     * @return 结果
     */
	@Override
	public int insertFantasyNpcPosition(FantasyNpcPosition fantasyNpcPosition)
	{
		Integer result = fantasyNpcPositionMapper.insertFantasyNpcPosition(fantasyNpcPosition);
		if (result > 0) {
			FantasyNpc fantasyNpc = fantasyNpcMapper.selectFantasyNpcById(fantasyNpcPosition.getNpcId());
			FantasyNpcPositionBO fantasyNpcPositionBO = new FantasyNpcPositionBO();
			fantasyNpcPositionBO.setMapId(fantasyNpcPosition.getMapId());
			fantasyNpcPositionBO.setNpcCommonTalkStatus(fantasyNpc.getNpcCommonTalkStatus());
			fantasyNpcPositionBO.setNpcCommonTaskStatus(fantasyNpc.getNpcCommonTaskStatus());
			fantasyNpcPositionBO.setNpcDesc(fantasyNpc.getNpcDesc());
			fantasyNpcPositionBO.setNpcFace(fantasyNpc.getNpcFace());
			fantasyNpcPositionBO.setNpcId(fantasyNpcPosition.getNpcId());
			fantasyNpcPositionBO.setNpcName(fantasyNpc.getNpcName());
			fantasyNpcPositionBO.setNpcReplyStatus(fantasyNpc.getNpcReplyStatus());
			fantasyNpcPositionBO.setNpcSpecialTalkStatus(fantasyNpc.getNpcSpecialTalkStatus());
			fantasyNpcPositionBO.setNpcSpecialTaskStatus(fantasyNpc.getNpcSpecialTaskStatus());
			fantasyNpcPositionBO.setPositionId(fantasyNpcPosition.getPositionId());
			fantasyNpcPositionBO.setTaskStatus(fantasyNpc.getNpcCommonTaskStatus());
			Object hGet = redisUtil.hGet(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPositionBO.getMapId());
			if (null != hGet) {
				List<FantasyNpcPositionBO> npclist = JSONObject.parseArray(String.valueOf(hGet), FantasyNpcPositionBO.class);
				npclist.add(fantasyNpcPositionBO);
				redisUtil.hPut(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPositionBO.getMapId(), JSON.toJSONString(npclist));
			}
		}
	    return result;
	}
	
	/**
     * 修改npc位置
     * 
     * @param fantasyNpcPosition npc位置信息
     * @return 结果
     */
	@Override
	public int updateFantasyNpcPosition(FantasyNpcPosition fantasyNpcPosition)
	{
		Integer result = fantasyNpcPositionMapper.updateFantasyNpcPosition(fantasyNpcPosition);
		if (result > 0) {
			FantasyNpc fantasyNpc = fantasyNpcMapper.selectFantasyNpcById(fantasyNpcPosition.getNpcId());
			FantasyNpcPositionBO fantasyNpcPositionBO = new FantasyNpcPositionBO();
			fantasyNpcPositionBO.setMapId(fantasyNpcPosition.getMapId());
			fantasyNpcPositionBO.setNpcCommonTalkStatus(fantasyNpc.getNpcCommonTalkStatus());
			fantasyNpcPositionBO.setNpcCommonTaskStatus(fantasyNpc.getNpcCommonTaskStatus());
			fantasyNpcPositionBO.setNpcDesc(fantasyNpc.getNpcDesc());
			fantasyNpcPositionBO.setNpcFace(fantasyNpc.getNpcFace());
			fantasyNpcPositionBO.setNpcId(fantasyNpcPosition.getNpcId());
			fantasyNpcPositionBO.setNpcName(fantasyNpc.getNpcName());
			fantasyNpcPositionBO.setNpcReplyStatus(fantasyNpc.getNpcReplyStatus());
			fantasyNpcPositionBO.setNpcSpecialTalkStatus(fantasyNpc.getNpcSpecialTalkStatus());
			fantasyNpcPositionBO.setNpcSpecialTaskStatus(fantasyNpc.getNpcSpecialTaskStatus());
			fantasyNpcPositionBO.setPositionId(fantasyNpcPosition.getPositionId());
			fantasyNpcPositionBO.setTaskStatus(fantasyNpc.getNpcCommonTaskStatus());
			Object hGet = redisUtil.hGet(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPositionBO.getMapId());
			if (null != hGet) {
				List<FantasyNpcPositionBO> npclist = JSONObject.parseArray(String.valueOf(hGet), FantasyNpcPositionBO.class);
				npclist.removeIf(filterNpcPositionBO -> filterNpcPositionBO.getNpcId().equals(fantasyNpcPosition.getNpcId()));
				npclist.add(fantasyNpcPositionBO);
				redisUtil.hPut(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPositionBO.getMapId(), JSON.toJSONString(npclist));
			}
		}
		return result;
	}

	/**
     * 删除npc位置对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFantasyNpcPositionByIds(String ids)
	{
		return fantasyNpcPositionMapper.deleteFantasyNpcPositionByIds(Convert.toStrArray(ids));
	}

	@Override
	public int deleteFantasyNpcPosition(FantasyNpcPosition fantasyNpcPosition) {
		Integer result = fantasyNpcPositionMapper.deleteFantasyNpcPosition(fantasyNpcPosition);
		if (result > 0) {
			Object hGet = redisUtil.hGet(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPosition.getMapId());
			List<FantasyNpcPositionBO> npclist = new ArrayList<>();
			if (null != hGet) {
				npclist = JSONObject.parseArray(String.valueOf(hGet), FantasyNpcPositionBO.class);
			}
			npclist.removeIf(fantasyNpcPositionBO -> fantasyNpcPositionBO.getNpcId().equals(fantasyNpcPosition.getNpcId()));
			if (npclist.isEmpty()) {
				redisUtil.hDelete(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPosition.getMapId());
			} else {
				redisUtil.hPut(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPosition.getMapId(), JSON.toJSONString(npclist));
			}
		}
		return result;
	}

	@Override
	public int deleteAllocNpcs(String mapId, String npcIds) {
		return fantasyNpcPositionMapper.deleteAllocNpcs(mapId, npcIds);
	}

	@Override
	public int insertAllocNpcs(String mapId, String npcIds, SysUser user) {
		String[] npcIdArray = Convert.toStrArray(npcIds);
		// 新增Npc位置
		List<FantasyNpcPosition> list = new ArrayList<>();
		for (String npcId : npcIdArray)
		{
			FantasyNpcPosition fantasyNpcPosition = new FantasyNpcPosition();
			fantasyNpcPosition.setPositionId(String.valueOf(IdWorker.nextId()));
			fantasyNpcPosition.setMapId(mapId);
			fantasyNpcPosition.setNpcId(npcId);// 取身份信息
			fantasyNpcPosition.setCreateBy(user.getUserId().toString());
			fantasyNpcPosition.setCreateTime(new Date());
			list.add(fantasyNpcPosition);
		}

		Integer result = fantasyNpcPositionMapper.batchAllocNpcs(list);
		if (result > 0) {
			for (FantasyNpcPosition fantasyNpcPosition : list) {
				FantasyNpc fantasyNpc = fantasyNpcMapper.selectFantasyNpcById(fantasyNpcPosition.getNpcId());
				FantasyNpcPositionBO fantasyNpcPositionBO = new FantasyNpcPositionBO();
				fantasyNpcPositionBO.setMapId(fantasyNpcPosition.getMapId());
				fantasyNpcPositionBO.setNpcCommonTalkStatus(fantasyNpc.getNpcCommonTalkStatus());
				fantasyNpcPositionBO.setNpcCommonTaskStatus(fantasyNpc.getNpcCommonTaskStatus());
				fantasyNpcPositionBO.setNpcDesc(fantasyNpc.getNpcDesc());
				fantasyNpcPositionBO.setNpcFace(fantasyNpc.getNpcFace());
				fantasyNpcPositionBO.setNpcId(fantasyNpcPosition.getNpcId());
				fantasyNpcPositionBO.setNpcName(fantasyNpc.getNpcName());
				fantasyNpcPositionBO.setNpcReplyStatus(fantasyNpc.getNpcReplyStatus());
				fantasyNpcPositionBO.setNpcSpecialTalkStatus(fantasyNpc.getNpcSpecialTalkStatus());
				fantasyNpcPositionBO.setNpcSpecialTaskStatus(fantasyNpc.getNpcSpecialTaskStatus());
				fantasyNpcPositionBO.setPositionId(fantasyNpcPosition.getPositionId());
				fantasyNpcPositionBO.setTaskStatus(fantasyNpc.getNpcCommonTaskStatus());
				Object hGet = redisUtil.hGet(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPositionBO.getMapId());
				List<FantasyNpcPositionBO> npclist = new ArrayList<>();
				if (null != hGet) {
					npclist = JSONObject.parseArray(String.valueOf(hGet), FantasyNpcPositionBO.class);
				}
				npclist.add(fantasyNpcPositionBO);
				redisUtil.hPut(RedisConstants.FANTASY_CITYMAP_NPC, fantasyNpcPositionBO.getMapId(), JSON.toJSONString(npclist));
			}
		}
		return result;
	}

}
